<?php
/**
 * 支付接口
 * @date    2021-01-01
 * @author  kiro
 * @email   294843009@qq.com
 * @version 1.0
 */
namespace App\Http\Controllers\Api\V1;

use App\Models\User;
use App\Models\UserVipLog;
use App\Models\UserVipShop;
use App\Services\PayService;
use App\Services\UserService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PayController extends ApiController
{
    // 支付发起
    public function do(Request $request)
    {
        if (empty($this->getUserId())) {
            return $this->error("请先登录", null, 500);
        }
        $mode = $request->get('mode');
        $pay_type = $request->get('pay_type');
        $amount = $request->get('amount');
        if (empty($amount)) {
            return $this->error("金额不能为空");
        }
        $pay_order_id = 0;
        PayService::do($this->getUserId(), $amount, $pay_type, $pay_order_id, 'AMOUNT');
        return $this->success('成功', []);
    }

    // 回调
    public function notify(Request $request)
    {
        $pay_type = $request->get('pay_type');
        $pay_order_id = 0;
        PayService::back($pay_type, $pay_order_id);
        return $this->success('成功', []);
    }

    // 支付发起
    public function doVip(Request $request)
    {
        if (empty($this->getUserId())) {
            return $this->error("请先登录", null, 500);
        }
        $mode = $request->get('mode');
        $pay_type = $request->get('pay_type');
        $amount = $request->get('amount');
        if (empty($amount)) {
            return $this->error("金额不能为空");
        }
        PayService::doVip($this->getUserId(), $amount, $pay_type, 0);
        return $this->success('成功', []);
    }

    // 回调
    public function notifyVip(Request $request)
    {
        $pay_type = $request->get('pay_type');
        $pay_order_id = 0;
        PayService::backVip($pay_type, $pay_order_id);
        return $this->success('成功', []);
    }

    //直接开通vip
    public function vip(Request $request)
    {
        if (empty($this->getUserId())) {
            return $this->error("请先登录", null, 500);
        }
        $pay_type = $request->get('pay_type', 'AMOUNT');
        $shop_id = $request->get('shop_id');
        if (empty($shop_id)) {
            return $this->error("shop_id不能为空");
        }
        $info = UserVipShop::where('id', $shop_id)->where('status', 1)->first();
        if ($info->type == 1) {

            DB::beginTransaction();
            UserService::doAccount($this->getUserId(), -$info->price, '余额支付', $pay_type, 'PAYMENT');

            $logId = UserVipLog::add($this->getUserId(), $shop_id, $info->price, 3, 0);
            try {
                $month = UserVipShop::where('id', $shop_id)->value('month');
                $vip_end_time = User::where('id', $this->getUserId())->value('vip_end_time');
                if (empty($month)) {
                    throw new \Exception("商品信息有问题");
                }

                if ($vip_end_time > strtotime(date('Y-m-d'))) {
                    $vip_end_time_new = strtotime("+{$month} month", $vip_end_time);
                } else {
                    $vip_end_time_new = strtotime("+{$month} month", strtotime(date('Y-m-d')));
                }
                $result = User::where('id', $this->getUserId())->update(['vip_end_time' => $vip_end_time_new]);
                if (!$result) {
                    throw new \Exception("更新过期时间失败");
                }
                UserVipLog::where('id', $logId)->update(['status' => 2]);
                DB::commit();
                return $this->success('成功', []);
            } catch (\Exception $e) {
                DB::rollBack();
                return $this->error($e->getMessage());
            }
        } else {
            return $this->error("暂不支持");
        }
    }
}
